package com.alex.DAO;

import java.util.List;



import com.alex.DAO.BaseDAO;
import com.alex.DAO.IGoodsDAO;
import com.alex.entity.Goods;
import org.hibernate.Query;
import org.hibernate.Session;

public class GoodsDAO extends BaseDAO implements IGoodsDAO{
	protected Integer catalogid;
	
	public Integer getCatalogid() {
		return catalogid;
	}

	public void setCatalogid(Integer catalogid) {
		this.catalogid = catalogid;
	}

	public List getGoodsByCatalogid(Integer catalogid){
	
		
		Session session=getSession();
		String hql="from Goods b where b.catalog.catalogid=?";
		Query query=session.createQuery(hql);
		query.setParameter(0, catalogid);
		List goods=query.list();
		session.close();
		
		
		return goods;
		
	}
	public List getGoodsByCatalogidPaging(Integer catalogid,int currentPage,int pageSize){
		Session session=getSession();
		Query query=session.createQuery("from Goods b where b.catalog.catalogid=?");
		query.setParameter(0, catalogid);
		int startRow=(currentPage-1)*pageSize;
		query.setFirstResult(startRow); 
		query.setMaxResults(pageSize);
		List goods=query.list();
		session.close();
		return goods;
	}
	public List getNewGoods(){
		Session session=getSession();
		Query query=session.createQuery("from Goods b");
		query.setFirstResult(0);
		query.setMaxResults(5);
		List goods=query.list();
		session.close();
		return goods;
	}
	public int getTotalByCatalog(Integer catalogid){
		Session session=getSession();
		Query query=session.createQuery("from Goods b where b.catalog.catalogid=?");
		query.setParameter(0,catalogid);
		List goods=query.list();
		int totalSize=goods.size();
		session.close();
		return totalSize;
	}
	
	public Goods getGoodsById(Integer goodsid){
		Session session=getSession();
		Goods goods=(Goods)session.get(Goods.class, goodsid);
		session.close();
		return goods;
	}
    public List getRequiredGoodsByHql(String hql) {
        
		try{
		Session session = getSession();
		Query query = session.createQuery(hql);
		List goods = query.list();
		System.out.println(hql);
		
		return goods;
		
		}catch(Exception e){
			System.out.println(e.getStackTrace());
			return null;
		}
		
	}

}
